Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
Listing 11.9 The File|Open command event procedure.
    Private Sub mnuFileOpen_Click()

    Dim Buffer1 As String, Buffer2 As String, CRLF As String
    Dim Reply As Integer, Flags As Integer, FileNum As Integer

    ‘ Verify that changes are saved, if user desires.

    Reply = SaveChanges()
    If Reply = False Then Exit Sub

    ‘ Get a filename from the user.

    CommonDialog1.FilterIndex = 1
    CommonDialog1.ShowOpen

    ‘ If user canceled.
    If CommonDialog1.FileName = “” Then Exit Sub
    FileName = CommonDialog1.FileName

    ‘ Open the file and read in text.
    FileNum = FreeFile
    Open FileName For Input As FileNum

    Do While Not EOF(FileNum)
        Line Input #FileNum, Buffer1
        Buffer2 = Buffer2 & Buffer1 & vbCRLF
    Loop

    Close FileNum

    ‘ Put the text in the Text Box.
    Text1.TEXT = Buffer2

    ‘ Display the filename in the form’s caption.
    frmBabyEditor.Caption = “Text Editor - ” & FileName

    ‘ Set flag.
    JustLoaded = True

    End Sub
#### Starting A New File The File|New command erases the existing contents of the editor (if any), leaving the user with a blank, unnamed document. Again, we use the SaveChanges function to guard against losing unsaved data. The operation of this function is quite straightforward. Listing 11.10 shows the File|New command event procedure. Listing 11.10 The File|New command event procedure.
    Private Sub mnuFileNew_Click()

    Dim Reply As Integer

    ‘ Verify that changes are saved, if user desires.
    Reply = SaveChanges()
    If Reply = False Then Exit Sub

    ‘ Erase the editor text and the filename.
    Text1.TEXT = “”
    FileName = “”

    ‘ Change the form caption and clear the TextChanged flag.
    frmBabyEditor.Caption = “Text Editor - Untitled”
    TextChanged = False

    End Sub
#### Saving A File Saving a file and ensuring against data loss is a bit more complicated, although proper planning simplifies the task. The two menu commands related to saving a file are File|Save and File|Save As. The first of these commands saves the file under its current name; if no name has been assigned yet, the user is prompted to enter one. The second command prompts the user for a file name regardless of whether it already has one. Remember, the global variable FileName holds the file name; this will be blank if the user has started the editor and entered some text without saving it, or if the user has selected the File|New command. We will design the code that does the actual job of saving the file (which we will call SaveFile ) to prompt the user for a file name only if the FileName variable is blank. Otherwise, the file will be saved under its existing name. We can then write the event procedures for the Save and Save As commands, as shown in Listings 11.11 and 11.12. Both event procedures call SaveFile. The difference is that the mnuFileSaveFileAs_Click procedure first sets FileName to a blank (saving its original value in the variable OldName ). Listing 11.11 The File|Save command event procedure.
    Private Sub mnuFileSave_Click()

    Call SaveFile

    End Sub
Listing 11.12 The File|Save As command event procedure.
    Private Sub mnuFileSaveFileAs_Click()

    OldName = FileName
    FileName = “”
    Call SaveFile

    End Sub
As you know, the procedure that does the actual saving is SaveFile, shown in Listing 11.13. Most of the code deals with the situation when FileName is blank. As we discussed a moment ago, this occurs only when the user is saving a new file or has selected the Save As command. We again call on the Common Dialog control, using the ShowSave method to display the Save dialog box. In this dialog box, the user can enter the desired file name, changing to a different folder if desired. The name is returned in the Common Dialog’s FileName property, which will be blank if the user canceled the dialog box. If the user entered a name, the file is saved under that name, and the form’s caption is changed accordingly. If the user canceled, FileName is set back to its old value (which was saved in OldName), and execution exits the procedure. The actual task of saving the file is, in many respects, identical to the procedure for reading a file. We use FreeFile to obtain an unused file number, and we open the file with the Open command—this time using the For Output qualifier, because we will be outputting data to the file. Unlike reading the file, however, we don’t have to output it one line at a time. We can save the entire buffer in one step with the Print command. Closing the file and clearing the TextChanged flag complete the necessary steps. The code is shown in Listing 11.13. Listing 11.13 The SaveFile procedure.
    Private Sub SaveFile()

    ‘ Saves current text under original filename.
    ‘ If no filename, prompts for one.

    Dim FileNum As Integer, Buffer As String

    Buffer = Text1.TEXT

    ‘ FileName will be blank only if we are saving a
    ‘ file for the first time or if user selected
    ‘ Save As.
    If FileName = “” Then
        CommonDialog1.ShowSave
        FileName = CommonDialog1.FileName

        If FileName = “” Then
            FileName = OldName
            Exit Sub
        Else
            frmBabyEditor.Caption = “Text Editor - ” + FileName
        End If
    End If

    FileNum = FreeFile

    Open FileName For Output As FileNum
    Print #FileNum, Buffer
    Close FileNum

    TextChanged = False

    End Sub
We finally arrive at the last file-related procedure, SaveChanges. This procedure’s job is to ensure that the user cannot lose unsaved changes to a file without warning. This procedure is called by both the File|Open and File|New event procedures. This is the first time you have seen a function, a type of procedure that returns a value to the calling program. You will also meet Visual Basic’s handy MsgBox function. Let’s cover these two new topics before diving into the function itself. Function Procedures In most respects, a function procedure is identical to the sub procedures we covered earlier. You create functions the same way as sub procedures, using the Insert Procedure command from the Visual Basic menu. Simply select the Function option in the Insert Procedure dialog box, and Visual Basic will create the function skeleton. You pass arguments to a function in the same way as you do for a sub procedure. If there are no arguments, leave the parentheses empty. When creating a function, you need to be concerned with two things that are not relevant to sub procedures. One is the data type of the value returned to the calling program, called the return type. A function can return any of Visual Basic’s data types, except a fixed-length string or an array. You declare the return type by placing an As clause after the function definition. For example, for a function named MyFunc that returns a type Long, the function skeleton would be:
    Function MyFunc() As Long

    End Function
If you omit the As clause, the return type defaults to Variant. The second concern is specifying the actual value to be returned. You accomplish this by assigning a value to the function name in code inside the function. For example, this code segment
    Function MyFunc() As Long

    ...
    MyFunc = x / y

    End Function
causes the function to return the result of dividing x by y. Note that assigning a return value does not terminate the function. Function execution continues until it reaches the terminating End Function statement or encounters an Exit Function statement in the body of the function. If you exit a function without assigning a return value, the function will return 0 or a blank string, depending on its type.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.